package com.bear.udisk.service.impl;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bear.udisk.mapper.UserinfoMapper;
import com.bear.udisk.model.Userinfo;
import com.bear.udisk.service.UserinfoService;
import com.bear.udisk.util.MyMD5Util;

@Service
public class UserinfoServiceImpl implements UserinfoService {
	@Autowired
	private UserinfoMapper uim;

	@Override
	public boolean register(Userinfo user) {
		// 1.对密码进行MD5加密
		String md5Pass = MyMD5Util.createMD5(user.getUserPass());
		user.setUserPass(md5Pass);
		// 2.将用户数据添加到数据中
		int count = uim.add(user);
		// 3.在服务端的上传目录中新建一个属于该用户的文件夹(利用用户名作为文件夹的名称)
		boolean isSuccess = false;
		File uploadPath = new File("D:/upload/" + user.getUserName());
		if (!uploadPath.exists()) {
			isSuccess = uploadPath.mkdirs();
		}
		if (count == 1 && isSuccess) {
			return true;
		}
		return false;
	}

	@Override
	public Userinfo login(String userName, String userPass) {
		// 1.对密码进行MD5加密
		String md5Pass = MyMD5Util.createMD5(userPass);
		// 2.利用加密后的密码进行查询
		Userinfo user = new Userinfo();
		user.setUserName(userName);
		user.setUserPass(md5Pass);
		Userinfo loginUser = uim.login(user);
		return loginUser;
	}

}
